package svl.videoplayer.player 
{
	import svl.videoplayer.player.CustomClient;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.NetStatusEvent;
	import flash.events.SecurityErrorEvent;
	import flash.media.Video;
	import flash.net.NetConnection;
	import flash.net.NetStream;	

	/**
	 * @author sanderl
	 */
	public class VideoPlayer extends Sprite 
	{
		private var netConnection:NetConnection;
		private var netStream:NetStream;
				
		public function VideoPlayer()
		{
			netConnection = new NetConnection();
			netConnection.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler, false, 0, true);
			netConnection.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler, false, 0, true);
			netConnection.connect(null);
		}
		
		private function securityErrorHandler(event:SecurityErrorEvent):void
		{
			trace("securityErrorHandler: " + event);
		}
		
		private function netStatusHandler(event:NetStatusEvent):void
		{
			switch (event.info.code) {
                case "NetConnection.Connect.Success":
                	trace("Connection succes");
                    connectStream();
                    break;
                case "NetStream.Play.StreamNotFound":
                    trace("Stream not found");
                    break;
            }
		}
		
		private function connectStream():void
		{
			netStream = new NetStream(netConnection);
            netStream.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
            netStream.client = new CustomClient();
           	var video:Video = new Video();
			video.width = 550;
			video.height = 400;
            video.attachNetStream(netStream);
            addChild(video);
		}

		public function getNetStream():NetStream
		{
			return netStream;
		}
	}
}
